package com.abc.webtest.controller;

import com.abc.webtest.db.entity.MainUser;
import com.abc.webtest.db.mapper.MainUserMapper;
import com.abc.webtest.util.HttpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TestController {
    @Resource
    MainUserMapper mainUserMapper;

    @Resource
    HttpUtil httpUtil;

    @GetMapping("/test")
    public String test() {
        return "hello";
    }

    @PostMapping("user/register") //定义post请求
    public Boolean userRegister(@RequestBody MainUser mainUser) {
        mainUserMapper.insert(mainUser);
        return true;
    }

    @GetMapping("user/info")
    public MainUser userInfo(Integer userId) {
        MainUser mainUser = new MainUser();
        return mainUserMapper.selectById(userId);
    }

    @GetMapping("inaword")
    public String inAWord() throws IOException {
        return httpUtil.get("http://api.lkblog.net/ws/api.php");
    }

    @GetMapping("create/random")
    public String createRandom(String name, String phone) throws IOException {
        Map<String, String> params = new HashMap<>();
        params.put("name", name);
        params.put("job", phone);
        return httpUtil.post("https://reqres.in/api/users", params);
    }
}
